package com.demo2do.fms.admin.web.controller.supplier.capacity;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.demo2do.core.entity.page.Page;
import com.demo2do.core.web.interceptor.MenuSetting;
import com.demo2do.core.web.utils.WebUtils;
import com.demo2do.fms.admin.entity.search.supplier.AbnormalCapacitySearch;
import com.demo2do.fms.admin.entity.security.Login;
import com.demo2do.fms.admin.service.supplier.VehicleLockService;
import com.demo2do.fms.admin.web.resolver.Secure;
import com.demo2do.fms.entity.vehicle.source.LockStatus;
import com.demo2do.fms.entity.vehicle.source.VehicleSourceLockRecord;

/**
 * 
 * 查询所有锁定和解锁记录
 * 
 * - 不包括冻结 - 冻结is_disabled = 1
 *
 * @author Ark
 *        
 */
@Controller
@RequestMapping("/abnormal-capacities")
@MenuSetting("menu-supplier")
public class AbnormalCapacityController {
    
    @Autowired
    private VehicleLockService vehicleLockService;
    
    /**
     * 异常运力列表
     * 
     * @param login
     * @param status
     * @param request
     * @param page
     * @return
     */
    @RequestMapping(value = "", method = RequestMethod.GET)
    @MenuSetting("submenu-abnormal-capacity-info")
    public ModelAndView list(@Secure Login login, @RequestParam(name = "status", required = false) LockStatus status,
                             HttpServletRequest request, Page page) {
            
        ModelAndView modelAndView = new ModelAndView("supplier/capacity/capacity/abnormal-capacity-list");
        modelAndView.addObject("status", status);
        
        int totals = vehicleLockService.countLockRecord(login, status);
        if ( totals > 0 ) {
            
            List<VehicleSourceLockRecord> vehicleSourceLockRecords = vehicleLockService.listLockRecord(login, status, page.with(totals));
            modelAndView.addObject("vehicleSourceLockRecords", vehicleSourceLockRecords);
            
            modelAndView.addObject("page", page);
            modelAndView.addObject("queryString", WebUtils.extractQueryString(request, "page", "filter"));
            
        }
        
        return modelAndView;
        
    }
    
    /**
     * search 异常运力查询
     * 
     * - 非供方，可做供方筛选
     * 
     * @param status
     * @param login
     * @return
     */
    @RequestMapping(value = "/search", method = RequestMethod.GET)
    @MenuSetting("submenu-abnormal-capacity-info")
    public ModelAndView search(@RequestParam(name = "status", required = false) LockStatus status,
                               @Secure Login login) {
            
        ModelAndView modelAndView = new ModelAndView("supplier/capacity/capacity/abnormal-capacity-search");
        
        modelAndView.addObject("status", status);
        modelAndView.addObject("supplier", login.isSupplier());
        
        return modelAndView;
        
    }
    
    /**
     * 查询异常运力记录
     * 
     * @param login
     * @param abnormalCapacitySearch
     * @param request
     * @param page
     * @return
     */
    @RequestMapping(value = "", method = RequestMethod.GET, params = "filter")
    @MenuSetting("submenu-abnormal-capacity-info")
    public ModelAndView onSearch(@Secure Login login, AbnormalCapacitySearch abnormalCapacitySearch,
                                 HttpServletRequest request, Page page) {
            
        ModelAndView modelAndView = new ModelAndView("supplier/capacity/capacity/abnormal-capacity-list");
        modelAndView.addObject("status", abnormalCapacitySearch.getStatus());
        
        int totals = vehicleLockService.countLockRecord(abnormalCapacitySearch, login);
        if ( totals > 0 ) {
            
            List<VehicleSourceLockRecord> vehicleSourceLockRecords = vehicleLockService.searchLockRecord(abnormalCapacitySearch, login, page.with(totals));
                    
            modelAndView.addObject("vehicleSourceLockRecords", vehicleSourceLockRecords);
            modelAndView.addObject("page", page);
            modelAndView.addObject("queryString", WebUtils.extractQueryString(request, "filter", "page"));
            
        }
        
        return modelAndView;
        
    }
    
}